package com.yiyuan.weixin.weixinInservice.Iml;

import com.yiyuan.weixin.model.ReceiveXmlEntity;
import com.yiyuan.weixin.utils.ReceiveXmlProcess;
import com.yiyuan.weixin.weixinInservice.service.WXAccessService;
import com.yiyuan.weixin.weixinInservice.service.WXResponseService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Created by admin on 2017/3/22.
 */
public class WeiXinAccessParentIml extends AbsWXAccessService {
    WXResponseService wxRespondService;
    @Override
    public void weiXinAccess(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String reqMessage=getStringFromWX(req);
        ReceiveXmlEntity xmlEntity = new ReceiveXmlProcess().getMsgEntity(reqMessage);
        wxRespondService.wxRespond(resp,xmlEntity);
//        responseMessage(resp,respMessage);

    }






//    @Override
//    public void weiXinAccess(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        req.setCharacterEncoding("UTF-8");
//        resp.setCharacterEncoding("UTF-8");
//
//        /** 读取接收到的xml消息 */
//        StringBuffer sb = new StringBuffer();
//        InputStream is = req.getInputStream();
//        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
//        BufferedReader br = new BufferedReader(isr);
//        String s = "";
//        while ((s = br.readLine()) != null) {
//            sb.append(s);
//        }
//        String xml = sb.toString(); //次即为接收到微信端发送过来的xml数据
//        System.out.println("解析之前："+xml);
//
//        String result = "";
//        /** 判断是否是微信接入激活验证，只有首次接入验证时才会收到echostr参数，此时需要把它直接返回 */
//        String echostr = req.getParameter("echostr");
//        if (echostr != null && echostr.length() > 1) {
//            result = echostr;
//        } else {
//            //正常的微信处理流程
//            result = new WechatProcess().processWechatMag(xml);
//        }
//
//        try {
//            OutputStream os = resp.getOutputStream();
//            os.write(result.getBytes("UTF-8"));
//            os.flush();
//            os.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println("格式化后："+result);
//    }



}
